#include "mybtn.h"
#include <stdio.h>


#if 0
static lv_obj_t * btn;

static void event_cb(lv_event_t* e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED)
        lv_obj_set_style_bg_color(btn, lv_color_hex(0xde4d3e),LV_STATE_DEFAULT);
    else if(code == LV_EVENT_VALUE_CHANGED)
        printf("11111\n");
}

static void btn_1(void)
{
    btn = lv_btn_create(lv_scr_act());
    lv_obj_set_size(btn, 100, 50);

    //设置按钮的坐标位置
    //lv_obj_set_pos(btn,400,240);//
    lv_obj_set_align(btn, LV_ALIGN_CENTER);//居中显示

    //1.点击按钮变色，释放恢复
    lv_obj_set_style_bg_color(btn, lv_color_hex(0xde4d3e),LV_STATE_PRESSED);

    //2.利用事件：点击后变色，释放保持不变
    lv_obj_add_event_cb(btn, event_cb, LV_EVENT_CLICKED, NULL);

    //3.添加标志，当按键状态变化，可产生LV_EVENT_VALUE_CHANGED这个事件
    lv_obj_add_flag(btn, LV_OBJ_FLAG_CHECKABLE);

    //注意事件类型
    lv_obj_add_event_cb(btn, event_cb, LV_EVENT_VALUE_CHANGED, NULL);
}
#endif

static lv_obj_t* label;
static lv_obj_t* btnAdd;
static lv_obj_t* btnSub;
static lv_obj_t* btnPause;
static int num = 0;

//定义速度显示标签
static void lv_label(void)
{
    label = lv_label_create(lv_scr_act());
    lv_label_set_text_fmt(label, "Speed: %d RPM",num);
    lv_obj_set_style_text_font(label, &lv_font_montserrat_20, LV_PART_MAIN);
    lv_obj_align(label, LV_ALIGN_CENTER,0,-200);
}

//事件处理函数
static void event_cb(lv_event_t* e)
{
    lv_obj_t* target = lv_event_get_target(e);
    if(target == btnAdd)
        num += 30;
    else if(target == btnSub)
        num -= 30;
    else if(target == btnPause)
    {
        lv_label_set_text_fmt(label, "Speed: %d RPM", 0);
        return ;
    }

    lv_label_set_text_fmt(label, "Speed: %d RPM", num);
}


//速度加
static void lv_btn_SpeedAdd(void)
{
    btnAdd = lv_btn_create(lv_scr_act());
    lv_obj_set_size(btnAdd, lv_obj_get_width(lv_scr_act())/4, lv_obj_get_height(lv_scr_act())/6);
    lv_obj_set_align(btnAdd, LV_ALIGN_LEFT_MID);

    lv_obj_t* btnAdd_label = lv_label_create(btnAdd);
    lv_label_set_text(btnAdd_label, "Speed+");
    lv_obj_set_style_text_font(btnAdd_label, &lv_font_montserrat_20, LV_PART_MAIN);
    lv_obj_set_align(btnAdd_label, LV_ALIGN_CENTER);

    lv_obj_add_event_cb(btnAdd, event_cb, LV_EVENT_CLICKED, NULL);
}

//速度减
static void lv_btn_SpeedSub(void)
{
    btnSub = lv_btn_create(lv_scr_act());
    lv_obj_set_size(btnSub, lv_obj_get_width(lv_scr_act())/4, lv_obj_get_height(lv_scr_act())/6);
    lv_obj_set_align(btnSub, LV_ALIGN_CENTER);

    lv_obj_t* btnSub_label = lv_label_create(btnSub);
    lv_label_set_text(btnSub_label, "Speed-");
    lv_obj_set_style_text_font(btnSub_label, &lv_font_montserrat_20, LV_PART_MAIN);
    lv_obj_set_align(btnSub_label, LV_ALIGN_CENTER);

    lv_obj_add_event_cb(btnSub, event_cb, LV_EVENT_CLICKED, NULL);
}

//终止
static void lv_btn_SpeedPause(void)
{
    btnPause = lv_btn_create(lv_scr_act());
    lv_obj_set_size(btnPause, lv_obj_get_width(lv_scr_act())/4, lv_obj_get_height(lv_scr_act())/6);
    lv_obj_set_align(btnPause, LV_ALIGN_RIGHT_MID);

    lv_obj_set_style_bg_color(btnPause,lv_color_hex(0xde1c31), LV_STATE_DEFAULT);
    lv_obj_t* btnPause_label = lv_label_create(btnPause);
    lv_label_set_text(btnPause_label, "Pause");
    lv_obj_set_style_text_font(btnPause_label, &lv_font_montserrat_20, LV_PART_MAIN);
    lv_obj_set_align(btnPause_label, LV_ALIGN_CENTER);

    lv_obj_add_event_cb(btnPause, event_cb, LV_EVENT_CLICKED, NULL);
}

//主程序
void btn_2(void)
{
    lv_label();
    lv_btn_SpeedAdd();
    lv_btn_SpeedSub();
    lv_btn_SpeedPause();
}

void mybtn_example(void)
{
    btn_2();
}
